package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.bean.GameDetailBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IGameDetailView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class GameDetailPresenter
{
    private BaseBean baseBean;
    private GameDetailBean gameDetailBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IGameDetailView iGameDetailView;
    private Context context;
    private BaseView baseView;
    private int requstType;
    public GameDetailPresenter(Context context, IGameDetailView iGameDetailView, BaseView baseView)
    {
        this.context = context;
        this.iGameDetailView = iGameDetailView;
        this.baseView=baseView;
    }

    /**
     * 比赛详情
     */
    public void getGameDetail()
    {
        requstType=1;
            MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
            myOkHttpClient.getGame(UrlData.BASEURL + UrlData.GAME, iGameDetailView.getGame_id(), handler);
    }
    /**
     * 加入比赛
     */
    public void joinGame()
    {
        requstType=2;
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.joinGame(UrlData.BASEURL + UrlData.JOINGAME, SharePrefencesUtil.getUser_id(context), iGameDetailView.getGame_id(), handler);
    }
    public GameDetailBean getData(){
        return gameDetailBean;
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        if(requstType==1){
                            Log.e("loginresult", result);
                            gameDetailBean= gson.fromJson(result, GameDetailBean.class);

                        }else if(requstType==2) {
                            baseBean=gson.fromJson(result,BaseBean.class);
                            if(0==baseBean.getState()){
                                Toast.makeText(context,"成功加入",Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(context,baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                            }
                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.showError(result);
                        break;
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    };

}